package com.hmall.mq;

import com.alibaba.fastjson.JSON;

import com.hmall.constants.MqConstants;
import com.hmall.dto.ItemBo;
import com.hmall.dto.ItemDoc;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ItemListener {

    @Autowired
    private RestHighLevelClient client;

    /**
     * 监听课程添加操作
     */
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = MqConstants.item_INSERT_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.item_EXCHANGE, type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true"), key = MqConstants.item_INSERT_KEY)})
    public void receiveCourseSaveMessage(String message) {
        try {

            ItemBo item = JSON.parseObject(message, ItemBo.class);
            //将消息转为课程，保存到es中
            // 2.转换为文档类型
            // ItemDoc itemDoc = new ItemDoc(item);

            ItemDoc itemDoc = new ItemDoc();
            itemDoc.setId(item.getId());

            itemDoc.setName(item.getName());
            itemDoc.setPrice(item.getPrice());
            itemDoc.setBrand(item.getBrand());
            itemDoc.setIsAD(item.getIsAD());
            itemDoc.setImage(item.getImage());
            itemDoc.setComment_count(item.getCommentCount());
            itemDoc.setSold(item.getSold());
            itemDoc.setCategory(item.getCategory());
            List<String> s = new ArrayList<>();
            s.add(item.getBrand());
            s.add(item.getCategory());
            itemDoc.setSuggestion(s);


            // 3.将HotelDoc转json
            String json = JSON.toJSONString(itemDoc);

            // 1.准备Request对象
            IndexRequest request = new IndexRequest("hmall").id(itemDoc.getId().toString());
            // 2.准备Json文档
            request.source(json, XContentType.JSON);
            // 3.发送请求
            client.index(request, RequestOptions.DEFAULT);
            //log.info("添加完成：{}",item);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 监听课程删除操作
     */
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = MqConstants.item_DELETE_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.item_EXCHANGE, type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true"), key = MqConstants.item_DELETE_KEY)})
    public void receiveCourseDeleteMessage(Long id) throws IOException {

        DeleteRequest request = new DeleteRequest("hmall", String.valueOf(id));
        // 2.发送请求

        client.delete(request, RequestOptions.DEFAULT);

    }
}